import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Rx';

import { Bird } from '../../model/bird-model';
import { Pillar } from '../../model/pillar-model';
import { Subscription } from '_rxjs@5.5.2@rxjs/Subscription';

//地图长宽比
const radio = 2;
//鸟的基本信息
const birdInfo = {
  left: 20,
  width: 5
}
//柱子的通用信息
const pillarInfo = {
  width: 4
}

@Injectable()
export class BirdFlyService {
  public initData = {
    //设置加速度
    g: 25,
    //设置初速度
    speed: 0,
    //设置反弹速度
    upSpeed: 30,
    //渲染间隔
    time: 20,
    //柱子移动距离，每秒钟移动15
    length: 15 / 1000 * 20
  };
  public birdStyle;
  public bird: Bird;
  public pillars: Pillar[];
  public subscriptions = [];
  public observers = [];

  //获取柱子信息
  public getPillars(): Pillar[] {
    return this.pillars;
  }
  //获取鸟的基本信息
  public getBird(): Bird {
    this.bird.x = birdInfo.left + birdInfo.width / 2;
    this.bird.y = this.birdStyle.topNum + birdInfo.width;
    return this.bird;
  }
  public initBirdStyle() {
    return this.birdStyle;
  }
  //获取鸟的数据
  public getBirdStyle(timePerMove: number) {
    this.birdStyle.topNum = this.birdStyle.topNum + this.getBirdMovedLength(1 / timePerMove);
    this.birdStyle.top = this.birdStyle.topNum + "%";
    if (this.birdStyle.topNum <= 0 || this.birdStyle.topNum + 10 >= 100) {
      this.over();
    }
    return this.birdStyle;
  }
  //获取渲染时间间隔
  public getTime(): number {
    return this.initData.time;
  }
  //获取柱子一次移动距离
  public getLength(): number {
    return this.initData.length;
  }
  //获取鸟移动距离
  public getBirdMovedLength(time: number) {
    this.initData.speed = this.initData.speed - this.initData.g * time;
    this.bird.status = this.initData.speed < 0 ? "down" : "up";
    return this.initData.g * time * time / 2 - this.initData.speed * time;
  }
  //鸟扑扇翅膀
  public birdFly(): void {
    if (this.bird.status == "over" || this.bird.status == "stop") {
      return;
    }
    this.initData.speed = this.initData.upSpeed;
  }

  //判断柱子是否到了临界区域
  public ifReach(pillar: Pillar): boolean {
    let pHalf = pillarInfo.width / 2;
    let pLeft = pillar.x - pHalf;
    let pRight = pillar.x + pHalf;
    let bHalf = birdInfo.width / 2;
    let bird = this.getBird();
    let bLeft = bird.x - bHalf;
    let bRight = bird.x + bHalf;
    return !(pLeft > bRight || pRight < bLeft);
  }
  public calcKnocked(pillar: Pillar): void {
    let bird = this.getBird();
    if (bird.y < pillar.y1 || bird.y > pillar.y2) {
      this.over();
      console.log('碰到柱子侧面');
    } else if (Math.abs(bird.x - pillar.x) < pillarInfo.width / 2
      && Math.min(Math.abs(bird.y - pillar.y1), Math.abs(bird.y - pillar.y2)) < birdInfo.width * radio / 2) {
      this.over();
      console.log('碰到柱子顶部');
    } else {
      let pHalf = pillarInfo.width / 2;
      let x1 = pillar.x - pHalf;
      let x2 = pillar.x + pHalf;
      let pointers = [{ x: x1, y: pillar.y1 }, { x: x2, y: pillar.y1 }, { x: x1, y: pillar.y2 }, { x: x2, y: pillar.y2 }];
      for (let p of pointers) {
        if ((p.x - bird.x) * (p.x - bird.x) + (p.y - bird.y) * (p.y - bird.y) / 4 < birdInfo.width * birdInfo.width / 4) {
          this.over();
          console.log('碰到柱子尖角');
          break;
        }
      }
    }
  }


  //添加观察者
  public addObserver(observer): number {
    this.observers.push(observer);
    return this.observers.indexOf(observer);
  }
  //设置订阅
  public addSubscription(observable): void {
    this.subscriptions = [];
    for (let observer of this.observers) {
      let subscription = observable.subscribe(observer);
      this.subscriptions.push(subscription);
    }
  }
  //获取订阅
  public cancleOneSubscription(i: number): void {
    this.subscriptions[i].unsubscribe();
  }
  //取消订阅
  public cancleSubscription(): void {
    for (let sub of this.subscriptions) {
      sub.unsubscribe();
    }
    console.log(this.getBird());
  }
  //初始化
  public init() {
    this.initData = {
      //设置加速度
      g: 25,
      //设置初速度
      speed: 0,
      //设置反弹速度
      upSpeed: 30,
      //渲染间隔
      time: 20,
      //柱子移动距离，每秒钟移动15
      length: 15 / 1000 * 20
    }
    this.bird = {
      name: 'zfjj',
      x: 0,
      y: 0,
      status: 'stop'
    };
    this.pillars = [];
    this.birdStyle = {
      'top': '20%',
      'topNum': 20,
      'left': birdInfo.left + '%',
      'width': birdInfo.width + '%',
      'height': birdInfo.width * 2 + '%'
    }
  }
  //开始
  public start() {
    this.init();
    this.bird.status = "down";
    let observable = Observable.interval(this.getTime());
    this.addSubscription(observable);
  }
  //结束
  public over() {
    this.bird.status = "over";
    this.initData.speed = 0;
    this.initData.g = 0;
    this.initData.length = 0;
  }
}
